package data_report

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/analysisClient/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GameCategoryReportLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 会员游戏分类报表
func NewGameCategoryReportLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GameCategoryReportLogic {
	return &GameCategoryReportLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GameCategoryReportLogic) GameCategoryReport(req *types.GameCategoryReportRequest) (resp types.GameCategoryReportReponse, err error) {

	resp = types.GameCategoryReportReponse{
		Data:     make([]types.GameCategoryReportData, 0),
		Page:     req.Page,
		PageSize: req.PageSize,
		Total:    0,
	}

	reqq := &rpc.SearchArGamesTypeAnalysisReq{
		Page:      req.Page,
		Limit:     req.PageSize,
		UserId:    req.UserId,
		Sort:      req.Sort,
		CreatedAt: req.StartTime,
		EndAt:     req.EndTime,
	}

	analysis, err := l.svcCtx.Analysis.SearchArGamesTypeAnalysis(l.ctx, reqq)
	if err != nil {
		return resp, err
	}

	for _, v := range analysis.ArGamesTypeAnalysis {

		t := types.GameCategoryReportData{
			UserId:              v.UserId,
			Balance:             v.Balance,
			ChargeAmount:        v.RechargeAmount,
			ChargeTimes:         int64(v.RechargeCount),
			WithdrawAmount:      v.WithdrawAmount,
			WithdrawTimes:       int64(v.WithdrawCount),
			BetAmount:           v.BetAmount,
			ServiceChagreAmount: v.ServiceChagreAmount,
			PayoutAmount:        v.PayoutAmount,
			ProfitAmount:        v.ProfitAmount,
			OrderNum:            v.OrderNum,
			CategoryCode:        v.CategoryCode,
			CategoryName:        v.CategoryName,
			GameCode:            v.GameCode,
			GameName:            v.GameName,
			CreatedAt:           v.CreatedAt,
			Type:                int(v.Type),
		}
		resp.Data = append(resp.Data, t)
	}
	resp.Total = analysis.Total
	return
}
